package com.demo.blog;

import com.demo.common.model.Goods;
import com.jfinal.plugin.activerecord.Page;
/**
 * 
 * @author ccx
 * @see 包裹goods的业务层
 */
public class GoodsService {
	
	private static final Goods dao = new Goods().dao();
	
	/**
	 * @param code 包裹的编号
	 * @param pageNumber 当前页码
	 * @param pageSize 最大显示信息数
	 * @param orderMode 排序的方式参数
	 * @return Page<Goods> Page对象，封装了页面参数，和数据List
	 * @see 根据特定的条件处理逻辑查询特定的包裹数据
	 */
	public Page<Goods> findObjects(String code,Integer pageNumber, Integer pageSize,String orderMode) {
		System.out.println(orderMode);
		
		/**
		 * 满足特定条件时查询所有并排序(页面加载后) 
		 */
		if((orderMode==null||orderMode.length()<=0)&&(code==null||code.length()<=0)){

			return dao.paginate(pageNumber, pageSize, "select *", "from goods order by id asc");
		}
		
/*		if((orderMode!=null&&orderMode.length()>0)&&(code!=null&&code.length()>0)){
			return dao.paginate(pageNumber, pageSize, "select *", "from goods where code = "+code+" order by weightime "+orderMode);
		}*/
		if(orderMode!=null&&orderMode.length()>0){
			return dao.paginate(pageNumber, pageSize, "select *", "from goods order by weightime "+orderMode);
		}
		
		/**
		 * 满足特定条件时查询并对结果进行重新排序(按时间)
		 */
/*		if((orderMode!=null&&orderMode.length()>0)&&(code!=null&&code.length()>0)){
			System.out.println("桩点1");
			Integer rows = Db.queryLong("select count(*) from goods where code=?",code).intValue();
			
			List<Goods> list = dao.find("select * from (select * from goods where code = "+code+" limit "
					+pageSize*(pageNumber-1)+","+pageSize+")"+" a order by weightime "+orderMode);
			
			System.out.println(list);
			Integer totalPage = rows/pageSize;
			if(rows%pageSize!=0){
				totalPage++;
			}
			Page<Goods> page = new Page<Goods>(list,pageNumber,pageSize,totalPage,rows);
			
			return page;
		}
		
		*//**
		 * 满足特定条件时查询并对结果进行重新排序(按时间)
		 *//*
		if(orderMode!=null&&orderMode.length()>0){
			System.out.println("桩点2");
			Integer rows = Db.queryLong("select count(*) from goods").intValue();
			
			List<Goods> list = dao.find("select * from (select * from goods limit "
					+pageSize*(pageNumber-1)+","+pageSize+")"+" a order by weightime "+orderMode);
			
			//这段出现业务bug,子查询出来的是一页数据，再对这一页数据去分页显示第二页的话是没有List,换成find();
//			List<Goods> list = dao.paginate(pageNumber, pageSize, "select *", "from (select * from goods order by id asc limit "
//					+pageSize*(pageNumber-1)+","+pageSize+")"+" a order by weightime "+orderMode).getList();
			
			System.out.println(list);
			Integer totalPage = rows/pageSize;
			if(rows%pageSize!=0){
				totalPage++;
			}
			Page<Goods> page = new Page<Goods>(list,pageNumber,pageSize,totalPage,rows);
			
			return page;
		}*/
		
		/**
		 * 满足特定条件时查询结果(按编号)
		 */
		return dao.paginate(pageNumber,pageSize, "select *", "from goods where code = "+code+" order by id asc");
		

	}
	/**
	 * @see 根据id查询数据
	 * @param id 传进来的id
	 * @return goods对象
	 */
	public Goods findById(Integer id) {
		System.out.println(dao.findById(id));
		return dao.findById(id);
	}
	/**
	 * @see 根据id更新数据
	 * @param goods对象
	 */
	public void updateById(Goods goods) {
		if(goods!=null){
			goods.update();
		}
	}

}

/*select * from (select * from goods order by id asc limit 0,10) a order by weightime desc limit 0,10 
startRow = pageSize*(pageNumber-1)*/
//Db.queryLong("select max(filedName) from tableName where condtion=?",1);
//Db.queryBigDecimal("select count(*) from goods");
//long i =(long)Db.queryNumber("select count(*) from goods").longValue();
//int a = Db.queryInt("select count(*) from goods");